package cn.wolfcode.springboot_producer._01_hello;


import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class SendMsgController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //同步
    @RequestMapping("/send")
    public String sendMsg(String msg) {
        rocketMQTemplate.syncSend("01-boot", msg);
        return "发送成功";
    }

    //异步
    @RequestMapping("/asyncSend")
    public String asyncSend(String msg) {
        rocketMQTemplate.asyncSend("01-boot", msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消息消费成功");
            }

            @Override
            public void onException(Throwable e) {
                System.out.println("消息消费失败");
            }
        });
        return "发送成功";
    }


    //一次性
    @RequestMapping("/oneSend")
    public String OneSend(String msg) {
        rocketMQTemplate.sendOneWay("01-boot", msg);
        return "发送成功";
    }

    //延时
    @RequestMapping("/sendDelay")
    public String sendDelay(String msg) {
        MessageBuilder<String> stringMessageBuilder = MessageBuilder.withPayload(msg);
        rocketMQTemplate.syncSend("send-delay", stringMessageBuilder.build(),5000,2);
        return "发送成功";
    }

   /* //Tag
    @RequestMapping("/sendFilter")
    public String sendFilter(String msg,String tag) {
        rocketMQTemplate.syncSend("boot-filter"+tag, msg);
        return "发送成功";
    }*/

    //自定义
    @RequestMapping("/sendFilter")
    public String sendFilter(String msg,String age) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("age", age);
        rocketMQTemplate.convertAndSend("boot-filter",msg , map);
        return "发送成功";
    }
}
